import axios from 'axios'
import QS from 'qs'
import * as CONSTANTS from '@/constants/index'
import { Toast } from 'vant'
import STORE from '@/store/index'

const instance = axios.create({ timeout: 12 * 1000 })

instance.defaults.headers.post['Content-Type'] =
  'application/x-www-form-urlencoded'

//请求拦截器
instance.interceptors.request.use(
  (config) => {
    const { state } = STORE;
    const { common } = state;    
    const token = common.token
    token && (config.headers.common['token'] = token)
    return config
  },
  (error) => Promise.error(error)
)

//响应拦截器
instance.interceptors.response.use(
  (res) => {
    if (res.status === 200) return Promise.resolve(res)
    return Promise.reject(res)
  },
  (err) => {
    const { response } = err
    if (!response.status) return Promise.reject(err.response)
  }
)

export default instance
